--[[
********************************************************************************
                    created :           2020/06/14
                    author   :           唐韡
                    purpose :           消息中心
********************************************************************************
--]]
require('LuaKits')

---事件类型（Enum）
EventType = {
    Player_Lv_Up_1 = 1,
    Player_Kill_Count_Add_1 = 2,
    Ping_Out = 3,
    COUNT = 4, --保留字
}

---事件中心（单例）
EventCenter = Singleton.GetInstance()
---事件容器
EventCenter.containner = {}
---注册
function EventCenter.Subscribe(eventType,callback)
    if not callback or type(callback)~='function' then
        print('Subscribe Error: Subscribe callback invaild.')
        return
    end
    if not EventCenter.containner[eventType] then
        EventCenter.containner[eventType] = {}
    end
    table.insert(EventCenter.containner[eventType],callback)
end
---注销
function EventCenter.Unsubscribe(eventType,callback)
    if not callback or type(callback)~='function' then
        print('Unsubscribe Error: Unsubscribe callback invaild.')
        return
    end
    if not EventCenter.containner[eventType] then
        print('Unsubscribe warnning ! eventType not be subscribed.')
    else
        for k,v in pairs(EventCenter.containner[eventType]) do
            if v == callback then
                EventCenter.containner[eventType][k] = nil
                if #EventCenter.containner[eventType] == 0 then
                    EventCenter.containner[eventType] = nil
                end
                return
            end
        end
    end
end
---发送
function EventCenter.Fire(eventType,...)
    if eventType >= EventType.COUNT then
        print('Fire Error ! 尚未定义的事件类型：'..eventType)
        return
    end
    if not EventCenter.containner[eventType] then
        print('Fire Warnning ! 尚未注册事件：'..eventType)
        return
    end
    for k,v in pairs(EventCenter.containner[eventType]) do
        v(...)
    end
end